﻿package sparrowGui.utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	/**
	 * 扩展了事件托管机制,增加批量帧听的等快捷操作
	 * @author pelephone
	 * @web http://hi.baidu.com/pelephone
	 * @email pelephone@163.com
	 */	
	public class EventDispatcherEx extends EventDispatcher implements IEventDispatcherEx
	{
		private var listenerMap:Object;
		public function EventDispatcherEx(target:IEventDispatcher=null)
		{
			super(target);
			listenerMap = {};
		}
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
			var ls:Array = listenerMap[type] as Array;
			if(!ls) listenerMap[type] = new Dictionary(useWeakReference);
			ls.push(listener);
		}
		
/*		override public function dispatchEvent(event:Event):Boolean
		{
			var ls:Array = listenerMap[event.type] as Array;
			if(!ls) return false;
			for each (var listener:Function in ls) 
			{
				listener.apply(null,[event]);
			}
			return true;
		}*/
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			super.removeEventListener(type,listener,useCapture);
			var ls:Array = listenerMap[type] as Array;
			if(!ls) return;
			var lid:int = ls.indexOf(listener);
			if(lid>=0) ls.splice(lid,1);
			if(!ls.length) delete listenerMap[type];
		}
		
		
		public function removeTypeListeners(type:String):void
		{
			listenerMap[type] = null;
			delete listenerMap[type];
		}
		
		public function removeAllListeners():void
		{
			listenerMap = {};
		}
		
		public function sendNote(s:String, args:Object=null, bubbles:Boolean=false, cancelable:Boolean=false):void
		{
			dispatchEvent(new EventEx(s,args,bubbles,cancelable));
		}
		
		public function dispose():void
		{
		}
	}
}